##########################################################################################

library(Seurat)
library(SeuratDisk)
library(optparse)

##########################################################################################

option_list <- list(
    make_option(c("--single_cell_all_file"), type = "character"),
    make_option(c("--single_cell_scissor_file"), type = "character"),
    make_option(c("--out_path"), type = "character")
)

if(1!=1){

    work_dir <- "~/20220915_gastric_multiple/dna_combinePublic"

    single_cell_all_file <- "~/20220915_gastric_multiple/dna_combinePublic/public_ref/singleCell/njmu/ALL_SIN_celltype.Rdata"
    single_cell_scissor_file <- paste0(work_dir,"/images/singleCell_MUC6/Scissor_STAD_MUC6_mutation.IM.CellRate.all.RData")
    out_path <- paste0("~/20220915_gastric_multiple/dna_combinePublic/images/singleCell_MUC6/cpdb")


}

##########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

out_path <- opt$out_path
single_cell_scissor_file <- opt$single_cell_scissor_file
single_cell_all_file <- opt$single_cell_all_file

dir.create(out_path , recursive = T)

##########################################################################################

a <- load(single_cell_scissor_file, verbose = F)
b <- load(single_cell_all_file, verbose = F)

##########################################################################################
## 提取用到的细胞，标记scissor阳性与阴性的pit细胞，所有的免疫细胞
pit_mut_cell <- names(which(sc_dataset$celltype=="Pit" & sc_dataset$scissor == 1))
pit_other_cell <- names(which(sc_dataset$celltype=="Pit" & sc_dataset$scissor != 1))

im_sc_dataset <- subset(ALL_SIN_celltype , 
    patient %in% "JZ732" & 
    sample == 'IM' & 
    (celltype %in% c("Bcell" , "Mast" , "Tcell") | celltype1 == "Pit" ) )

sc_dataset <- im_sc_dataset

##########################################################################################
## Pit突变相关细胞
sc_dataset$celltype1[names(sc_dataset$celltype1) %in% pit_mut_cell] <- "Pit_Mut"
## Pit其它细胞
sc_dataset$celltype1[names(sc_dataset$celltype1) %in% pit_other_cell] <- "Pit_Other"
sc_dataset$celltype <- sc_dataset$celltype1

sc_dataset <- NormalizeData(object = sc_dataset, normalization.method = 'LogNormalize',scale.factor = 10000)
sc_dataset <- FindVariableFeatures(object = sc_dataset,selection.method='vst',mean.function = ExpMean,dispersion.function = LogVMR,mean.cutoff=c(0.125,3),dispersion.cutoff=c(0.5,Inf))
sc_dataset <- ScaleData(sc_dataset,vars.to.regress = c("percent.mt","nCount_RNA"))
sc_dataset <- RunPCA(object=sc_dataset,npcs=100,pc.genes=VariableFeatures(object=sc_dataset))

pcSelect=25
sc_dataset <- FindNeighbors(object=sc_dataset,dims=1:pcSelect)
sc_dataset <- FindClusters(object=sc_dataset,resolution=c(0.1,0.2,0.3,0.5,0.8))
sc_dataset <- RunUMAP(sc_dataset, dims = 1:pcSelect,min.dist = 0.5, n.neighbors = 25L)

out_name <- paste0( out_path , "/IM_MUC6.Pit_Immune.h5seurat" )
SaveH5Seurat(sc_dataset , filename=out_name, overwrite = TRUE)
Convert(out_name, dest = "h5ad", overwrite = TRUE)
